//
//  ZZTongErrorView.m
//  HPZZTong
//
//  Created by Handpay on 14-3-29.
//  Copyright (c) 2014年 Handpay. All rights reserved.
//

#import "ZZTongErrorView.h"
#import "ZZTongDefine.h"
#import "DefineUtil.h"

@interface ZZTongErrorView (){
    CAShapeLayer *_shapeLayer;
}
@property(nonatomic, retain)UILabel* msgLabel;
@end

@implementation ZZTongErrorView

#define Corner_Radius 5

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (id)init{
    self = [super init];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    
    [self initialize];
}

- (void)initialize
{
    UIImageView* tagImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 22, 22)];
    UIImage* tagImage = [UIImage imageNamed:@"warn.png"];
    tagImageView.image = tagImage;
    [self addSubview:tagImageView];
    [tagImageView release];
    
    UILabel* tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(42, 10, 243, 21)];
    tempLabel.font = [UIFont systemFontOfSize:14];
    tempLabel.textColor = UIColorFromRGB(0xFF5200);
    tempLabel.numberOfLines = 0;
    self.msgLabel = tempLabel;
    [self addSubview:self.msgLabel];
    [tempLabel release];
}

- (void)drawDashedBorder
{
    if (_shapeLayer) [_shapeLayer removeFromSuperlayer];
    
	CGRect frame = self.bounds;
    
	_shapeLayer = [CAShapeLayer layer];
    
    //creating a path
	CGMutablePathRef path = CGPathCreateMutable();
    
    //drawing a border around a view
	CGPathMoveToPoint(path, NULL, 0, frame.size.height - Corner_Radius);
    CGPathAddLineToPoint(path, NULL, 0, Corner_Radius);
    CGPathAddArc(path, NULL, Corner_Radius, Corner_Radius, Corner_Radius, M_PI, -M_PI_2, NO);
    CGPathAddLineToPoint(path, NULL, frame.size.width - Corner_Radius, 0);
    
	CGPathAddArc(path, NULL, frame.size.width - Corner_Radius, Corner_Radius, Corner_Radius, -M_PI_2, 0, NO);
	CGPathAddLineToPoint(path, NULL, frame.size.width, frame.size.height - Corner_Radius);
	CGPathAddArc(path, NULL, frame.size.width - Corner_Radius, frame.size.height - Corner_Radius, Corner_Radius, 0, M_PI_2, NO);
	CGPathAddLineToPoint(path, NULL, Corner_Radius, frame.size.height);
	CGPathAddArc(path, NULL, Corner_Radius, frame.size.height - Corner_Radius, Corner_Radius, M_PI_2, M_PI, NO);
    
    //path is set as the _shapeLayer object's path
	_shapeLayer.path = path;
	CGPathRelease(path);
    

    _shapeLayer.strokeColor = [UIColorFromRGB(0xFF5200) CGColor];
    self.backgroundColor = [UIColor whiteColor];
    
	_shapeLayer.frame = frame;
	_shapeLayer.masksToBounds = NO;
	[_shapeLayer setValue:[NSNumber numberWithBool:NO] forKey:@"isCircle"];
	_shapeLayer.fillColor = [[UIColor clearColor] CGColor];

	_shapeLayer.lineWidth = 1;
//	_shapeLayer.lineDashPattern = self.borderType == BorderTypeDashed ? [NSArray arrayWithObjects:[NSNumber numberWithInt:self.dashPattern], [NSNumber numberWithInt:self.spacePattern], nil] : nil;
	_shapeLayer.lineCap = kCALineCapRound;
    
    //_shapeLayer is added as a sublayer of the view
	[self.layer addSublayer:_shapeLayer];
	self.layer.cornerRadius = Corner_Radius;
}

- (void)setErrorMsg:(NSString *)errorMsg{
    self.msgLabel.text = errorMsg;
    [self.msgLabel sizeToFit];
    
    CGRect frame = self.frame;
    frame.size.height = self.msgLabel.frame.origin.y + self.msgLabel.frame.size.height + 10;
    self.frame = frame;
}

- (void)refresh{
    [self drawDashedBorder];
}

- (void)dealloc{
    self.errorMsg = nil;
    self.msgLabel = nil;
    
    [super dealloc];
}

@end
